package com.mbigger.wechat.mp.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.mbigger.wechat.mp.config.WxMpConfiguration;

import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;

@RestController
@RequestMapping("/wx/templateMsg/{appid}")
public class WxTemplateMessageController {

	@RequestMapping("/send")
	public String send(@PathVariable String appid) {

		WxMpService mpService = WxMpConfiguration.getMpServices().get(appid);
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
		WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder().toUser("oeser1Xzp-oM8ux79Wp017jUiIGE")
				.templateId("5YzREFAAX1Q0Cg8ZuN_sx8r5L2_Zikd82BY989Ig6Ow")
				.url("http://28qpve.natappfree.cc/wx/auth/" + appid + "/index").build();

		templateMessage.addData(new WxMpTemplateData("first", "电站故障", "#FF00FF"))
				.addData(new WxMpTemplateData("keyword1", "东北角乙烯变", "#FF00FF"))
				.addData(new WxMpTemplateData("keyword2", "脱硫脱氨器", "#FF00FF"))
				.addData(new WxMpTemplateData("keyword3", "气压越上限", "#FF00FF"))
				.addData(new WxMpTemplateData("keyword4", dateFormat.format(new Date()), "#FF00FF"))
				.addData(new WxMpTemplateData("remark", "请及时处理此故障，已处理请忽略", "#FF00FF"));
		String msgId = "error";
		try {
			msgId = mpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
		} catch (WxErrorException e) {
			e.printStackTrace();
		}
		return msgId;
	}
}
